import { Packet } from 'packet-model.js';
var packet = new Packet();
import { Config } from '../../utils/config.js';
Page({

  data: {

  },
  onLoad: function (options) {
   this.data.id = options.id ? options.id : '';
    if (!this.data.id){
      wx.showToast({
        title: '参数有误',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    var config = wx.getStorageSync('config');
    this.data.site_name = config[2].value;
    this.detail();
  },
  //红包详情
  detail: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var args = {
      id: that.data.id,
    };
    packet.detail(args, (data) => {
      var that = this;
      wx.hideLoading();
      if (data.status == 1 || data.status == 2)
      {
        if (data.data.info) {
          data.data.info = JSON.parse(data.data.info);
        }
        that.setData({
          item: data.data,
          onLoading: false,
        })
      } else if (data.status == 0){
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }
      that.setData({
        status : data.status
      }) 

    })
  },
  //分享红包
  onShareAppMessage: function (res) {
    var that = this;
    var name = that.data.site_name ? that.data.site_name : '首页';
    if (res.from != 'button') {
      return {
        title: name,
        path: '/pages/home/home',
      }
    } else {
      if (!that.data.item.code || !that.data.item.id) {
        return false;
      }
      return {
        title: '我在' + name +'发了一个红包，快来领取吧',
        imageUrl: Config.domain+'/storage/setting/share.png',
        path: '/pages/packet/receive?code=' + that.data.item.code + '&id=' + that.data.item.id,
      }
    }
  }
})